﻿using JESAI.Redis.Consts;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace JESAI.Redis.Event
{
    public static class EventBus
    {
        private static readonly DiagnosticListener DiagnosticListener = new(RedisConst.DiagnosticListenerName);

        /// <summary>
        /// 写入缓存前
        /// </summary>
        /// <returns></returns>
        internal static void WriteRedisBefore(string cmd, object[] args)
        {
            string name = "WriteRedisEventBefore";
            if (!DiagnosticListener.IsEnabled(name))
                return;
            DiagnosticListener.Write(name, new { cmd = cmd, args = args });
        }

        /// <summary>
        /// 写入缓存后
        /// </summary>
        /// <returns></returns>
        internal static void WriteRedisAfter(string cmd, object[] args, object result)
        {
            string name = "WriteRedisEventAfter";
            if (!DiagnosticListener.IsEnabled(name))
                return;
            DiagnosticListener.Write(name, new { cmd = cmd, args = args, result = result });
        }

        /// <summary>
        /// 选择客户端触发的错误
        /// </summary>
        /// <returns></returns>
        internal static void SelectRedisClientError(string host, int port, Exception exception,
            string eventName = null)
        {
            if (eventName == null)
            {
                eventName = "SelectRedisClientErrorEventData";
            }
            var name = "SelectRedisClientErrorEvent";
            if (!DiagnosticListener.IsEnabled(name))
                return;
            DiagnosticListener.Write(name, new { host, port, exception, eventName });
        }

        /// <summary>
        /// 开启流水线
        /// </summary>
        /// <returns></returns>
        internal static void BeginPipe(string host, int port)
        {
            string name = "BeginPipeEvent";
            if (!DiagnosticListener.IsEnabled(name))
                return;
            DiagnosticListener.Write(name, new { host, port });
        }

        /// <summary>
        /// 结束流水线
        /// </summary>
        /// <returns></returns>
        internal static void EndPipe(string host, int port, object result)
        {
            string name = "EndPipeEvent";
            if (!DiagnosticListener.IsEnabled(name))
                return;
            DiagnosticListener.Write(name, new { host, port, result });
        }

        /// <summary>
        /// 接收订阅消息处理
        /// </summary>
        /// <returns></returns>
        internal static void ReceiveSub(object result)
        {
            string name = "ReceiveSubEvent";
            if (!DiagnosticListener.IsEnabled(name))
                return;
            DiagnosticListener.Write(name, new { result });
        }

        /// <summary>
        /// 开启事务
        /// </summary>
        /// <returns></returns>
        internal static void BeginTran(string host, int port)
        {
            string name = "BeginTranEvent";
            if (!DiagnosticListener.IsEnabled(name))
                return;
            DiagnosticListener.Write(name, new { host, port });
        }

        /// <summary>
        /// 结束事务
        /// </summary>

        /// <returns></returns>
        internal static void EndTran(string host, int port, object result)
        {
            string name = "EndTranEvent";
            if (!DiagnosticListener.IsEnabled(name))
                return;
            DiagnosticListener.Write(name, new { host, port, result });
        }

        /// <summary>
        /// 取消事务
        /// </summary>
        /// <returns></returns>
        internal static void DiscardTran(string host, int port)
        {
            string name = "DiscardTranEvent";
            if (!DiagnosticListener.IsEnabled(name))
                return;
            DiagnosticListener.Write(name, new { host, port });
        }

        /// <summary>
        /// 发送哨兵消息
        /// </summary>
        /// <returns></returns>
        internal static void SendSentinel(string host, int port, string cmd, object[] argv)
        {
            string name = "SendSentinelEvent";
            if (!DiagnosticListener.IsEnabled(name))
                return;
            DiagnosticListener.Write(name, new
            {
                host,
                port,
                cmd,
                argv
            });
        }

        /// <summary>
        /// 接收哨兵消息
        /// </summary>
        /// <returns></returns>
        internal static void ReceiveSentinel(string host, int port, string cmd, object[] argv, object result)
        {
            string name = "ReceiveSentinelEvent";
            if (!DiagnosticListener.IsEnabled(name))
                return;
            DiagnosticListener.Write(name, new
            {
                host,
                port,
                cmd,
                argv,
                result
            });
        }

        /// <summary>
        ///哨兵错误
        /// </summary>
        /// <returns></returns>
        internal static void SentinelMessageError(string host, int port, Exception exception)
        {
            string name = "SentinelErrorEvent";
            if (!DiagnosticListener.IsEnabled(name))
                return;
            DiagnosticListener.Write(name, new { host, port, exception });
        }

        /// <summary>
        ///创建锁成功
        /// </summary>
        /// <returns></returns>
        internal static void LockCreateSuccess(string resourceName, string lockId)
        {
            string name = "LockCreateSuccessEvent";
            if (!DiagnosticListener.IsEnabled(name))
                return;
            DiagnosticListener.Write(name, new { resourceName, lockId });
        }

        /// <summary>
        ///获取锁失败
        /// </summary>
        /// <returns></returns>
        internal static void LockCreateFail(string resourceName, string lockId)
        {
            string name = "LockCreateFailEvent";
            if (!DiagnosticListener.IsEnabled(name))
                return;
            DiagnosticListener.Write(name, new { resourceName, lockId });
        }

        /// <summary>
        ///锁报错
        /// </summary>
        /// <returns></returns>
        internal static void LockCreateException(string resourceName, string lockId, Exception exception)
        {
            string name = "LockCreateExceptionEvent";
            if (!DiagnosticListener.IsEnabled(name))
                return;
            DiagnosticListener.Write(name, new { resourceName, lockId, exception });
        }

        ///  <summary>
        /// 客户端缓存执行异常
        ///  </summary>
        ///  <param name="clientId">客户端id</param>
        ///  <param name="result">返回结果</param>
        ///  <returns></returns>
        internal static void ClientSideCachingStart(string clientId, object result)
        {
            string name = "ClientSideCachingStartEvent";
            if (!DiagnosticListener.IsEnabled(name))
                return;
            DiagnosticListener.Write(name, new { clientId, result });
        }

        ///  <summary>
        /// 客户端缓存删除
        ///  </summary>
        ///  <param name="key">删除的key</param>
        ///  <returns></returns>
        internal static void ClientSideCachingRemove(string key)
        {
            string name = "ClientSideCachingRemoveEvent";
            if (!DiagnosticListener.IsEnabled(name))
                return;
            DiagnosticListener.Write(name, new { key });
        }

        ///  <summary>
        /// 客户端缓存更新
        ///  </summary>
        ///  <param name="key">删除的key</param>
        ///  <param name="value"></param>
        ///  <returns></returns>
        internal static void ClientSideCachingUpdate(string key, object value)
        {
            string name = "ClientSideCachingUpdateEvent";
            if (!DiagnosticListener.IsEnabled(name))
                return;
            DiagnosticListener.Write(name, new { key, value });
        }

        /// <summary>
        ///客户端缓存执行异常
        /// </summary>
        /// <param name="exception"></param>
        /// <returns></returns>
        internal static void ClientSideCachingException(Exception exception)
        {
            string name = "ClientSideCachingExceptionEvent";
            if (!DiagnosticListener.IsEnabled(name))
                return;
            DiagnosticListener.Write(name, new { exception });
        }
    }
}
